package com.ngovanhau.justsort;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

import com.ngovanhau.justsort.item.config.MyConfig;

public class ProgressBar extends MySprite {
	/**
	 * Tạm dừng hoặc tiếp tục
	 */
	boolean isPause = false;
	boolean isStop = false;
	Rectangle mRectangle;

	private BitmapTextureAtlas prb_BTA;
	private Sprite prb_SP;
	private TextureRegion prb_TR;
	int time_tmp = 0;

	/**
	 * Tổng thời gian
	 */
	int total_time = -1;

	public static float width_rect = 0;

	public int getTime() {
		return total_time - time_tmp;
	}

	public void onLoadResources(Engine mEngine, Context mContext) {
		this.engine = mEngine;
		this.context = mContext;
		this.mainGame = (MainGameActivity) mContext;

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
		this.prb_BTA = new BitmapTextureAtlas(512, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.prb_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.prb_BTA, mContext, "progressbar.png", 0, 0);
		mEngine.getTextureManager().loadTextures(this.prb_BTA);
	}

	public void onLoadScene(Scene mScene) {
		this.scene = mScene;
		int y = (int) (60 * MyConfig.RACE_HEIGHT);
		int x = (MyConfig.SCREENWIDTH - (int) (prb_TR.getWidth() * MyConfig
				.getRaceWidth())) / 2;

		int h = (int) (prb_TR.getHeight() * MyConfig.getRaceHeight())
				- (int) (10 * MyConfig.getRaceHeight());

		int w = (int) (prb_TR.getWidth() * MyConfig.getRaceWidth());
		h = prb_TR.getHeight() * w / prb_TR.getWidth();

		this.prb_SP = new Sprite(x, y, w, h, this.prb_TR);
		this.scene.attachChild(prb_SP);

		mRectangle = new Rectangle(x, y, w, h);
		mRectangle.setColor(0f, 0.83f, 0.02f);
		this.scene.attachChild(mRectangle);

		mRectangle.setZIndex(100);
		scene.sortChildren();

		width_rect = w;
	}

	public void reload() {
		mRectangle.setVisible(true);
		prb_SP.setVisible(true);
		isStop = false;
		isPause = false;
		mRectangle.setWidth(width_rect);
	}

	public void setPause(boolean isPause) {
		this.isPause = isPause;
	}

	public void setStop(boolean isStop) {
		this.isStop = isStop;
	}

	public void setTotalTime(int total_time) {
		this.total_time = total_time;
	}

	public void updateProgressBar(float value) {
		mRectangle.setWidth(mRectangle.getWidth() - value);
	}
}
